<?php
/**
 * @author Nguyen Thanh Tung
 * @copyright 2014
 * @description: save action sample. 
 */
// not login
// not perm add
if(!isset($AppUI->id)||
!isset($_SESSION['user'])||
(isset($_SESSION['user'])&&($_SESSION['user']&1<<1)==0))
 {
    redirect(HOST_BACKEND);
}
$this->load->model('user_model', 'user');
// data input
$strUsername         = $this->input->post('pusername',null); 
$strPassword    	 = $this->input->post('ppassword',null);			
$strEmail    	     = $this->input->post('pemail',null); 
$strFirstname        = $this->input->post('pfirstname', null);
$strLastname         = $this->input->post('plastname', null);
$strBirthday         = $this->input->post('pbirthtday', null);
$intGender           = (int)$this->input->post('pgender', 0);
$intStatus           = (int)$this->input->post('pstatus', 0);
$strCountry          = $this->input->post('pcountry', null);
$intRole             = (int)$this->input->post('pprole', 0);
$arrErr             = array();
// strip tag data input
$strUsername        = CI_Function::stripTag($strUsername);
$lenUsername        = strlen($strUsername);

$lenPassword        = strlen($strPassword);
$strEmailCheck		= CI_Function::isEmail($strEmail);

$strFirstname        = CI_Function::stripTag($strFirstname);
$lenFirstname         = strlen($strFirstname);

$strLastname        = CI_Function::stripTag($strLastname);
$lenLastname         = strlen($strLastname);
// validate manual	
//+ username
//$checkUsername=$this->user->getInfoUsername($strUsername);		
if(empty($strUsername))
    array_push($arrErr, $arrLocale['username_required']);
elseif($lenUsername < VL_USERNAME_MINLENGTH || $lenUsername > VL_USERNAME_MAXLENGTH)
    array_push($arrErr, $arrLocale['username_range']);
elseif(!CI_Function::isUsername($strUsername))
    array_push($arrErr, $arrLocale['username_notmatch']);
//+ email
if(empty($strEmail)) {
    array_push($arrErr, $arrLocale['email_required']);
}elseif(!CI_Function::isEmail($strEmail) && $strEmail != '') {
    array_push($arrErr, $arrLocale['email_check']);
}	
//+ password
if(empty($strPassword))
    array_push($arrErr, $arrLocale['password_required']);
elseif($lenPassword < VL_PASSWORD_MINLENGTH) 
    array_push($arrErr, $arrLocale['password_minlength']);
//+firstname 
if(empty($strFirstname))
    array_push($arrErr, $arrLocale['firstname_required']);
elseif(($lenFirstname < 2 || $lenFirstname > VL_USERNAME_MAXLENGTH) ) 
    array_push($arrErr, $arrLocale['firstname_range']);
//+lastname 
if(empty($strLastname))
    array_push($arrErr, $arrLocale['lastname_required']);
elseif(($lenLastname < 2 || $lenLastname > VL_USERNAME_MAXLENGTH) ) 
    array_push($arrErr, $arrLocale['lastname_range']);
   
if(count($arrErr)==0) {//allow save
    // unset session if exists
    if(isset($_SESSION['USER_ERROR'])) unset($_SESSION['USER_ERROR']);
    if(isset($_SESSION['USER_POST'])) unset($_SESSION['USER_POST']);
    $intError = $this->user->add($AppUI->id, $strUsername, md5($strPassword), $strEmail, $strFirstname, $strLastname,
					CI_Function::convertStrToDate($strBirthday), $intGender, $intStatus, $strCountry, $intRole);

	//test($intError);				
    if($intError==0) CI_Function::setMsg($arrMessage['save_add']);
    elseif($intError==1) array_push($arrErr, $arrError['noperm_user_add']);
    elseif($intError==2) array_push($arrErr, $arrLocale['username_validate']);        
    elseif($intError==3) array_push($arrErr, $arrLocale['email_match']);        
}
// occur error

if (isset($arrErr) && count($arrErr) > 0) {
    $_SESSION['USER_ERROR']    = $arrErr;    
    $_SESSION['USER_POST']     = $this->input->post();
    
    redirect(HOST_BACKEND . 'user/add');
}
redirect(HOST_BACKEND);
